import 'package:cloud_medicine/components/shop_item.dart';
import 'package:cloud_medicine/components/util.dart';
import 'package:cloud_medicine/http/config.dart';
import 'package:cloud_medicine/http/shop_api.dart';
import 'package:cloud_medicine/model/shop_list.dart';
import 'package:cloud_medicine/pages/shop_detail.dart';
import 'package:flutter/material.dart';
import 'package:left_scroll_actions/leftScroll.dart';

class ShopManage extends StatefulWidget {
  ShopManage({Key key}) : super(key: key);

  @override
  _ShopManageState createState() => _ShopManageState();
}

class _ShopManageState extends State<ShopManage> {
  bool _loading = false;
  List<ShopItem> myShopModel = [];
  Future<Null> _init() async {
    setState(() {
      _loading = true;
    });
    dynamic res = await getShopList_api('$backIP/shop/shopList'); //接口路径
    res['data'].forEach((dynamic shopItem) {
      //遍历接口中的数据
      ShopItem shopItemModel = ShopItem.init(shopItem);
      myShopModel.add(shopItemModel);
    });
    if (mounted) {
      setState(() {
        _loading = false;
      });
    }
    return;
  }

  @override
  void initState() {
    super.initState();
    _init();
  }

  popMenuItem(IconData icon, String text, String cvalue) {
    return PopupMenuItem<String>(
        value: cvalue,
        child: Row(
          mainAxisAlignment: MainAxisAlignment.spaceEvenly,
          children: <Widget>[Icon(icon, color: Colors.blue), Text(text)],
        ));
  }

  Future _deleteShop(ShopItem e) async {
    await deleteShop_api('$backIP/shopManage/delete', {"ID": e.id.toString()});
  }

  Widget _childLayout() {
    if (_loading) {
      return Center(
        child: Container(
          child: CircularProgressIndicator(),
        ),
      );
    } else if (myShopModel.isEmpty) {
      return Align(
          alignment: Alignment.topCenter,
          child: Container(
            margin: EdgeInsets.only(top: 10),
            child: Text(
              '还没有上架药店哦~',
              style: TextStyle(color: Colors.grey[500]),
            ),
          ));
    } else {
      return ListView.builder(
        itemCount: myShopModel.length,
        itemBuilder: (context, index) {
          return LeftScroll(
              buttons: <Widget>[
                LeftScrollItem(
                  text: '下架',
                  color: Colors.red,
                  onTap: () {
                    setState(() {
                      openAlertDialog(context, () {
                        setState(() {
                          _deleteShop(myShopModel[index]);
                          myShopModel.removeAt(index);
                        });
                      }, '确认下架此药店吗？一旦下架，不可撤销！');
                    });
                  },
                ),
                LeftScrollItem(
                  text: '管理',
                  color: Colors.yellow[600],
                  onTap: () {
                    Navigator.of(context).push(MaterialPageRoute(
                        builder: (BuildContext context) => ShopDetail(
                              shopid: myShopModel[index].id,
                              isManage: true,
                            )));
                  },
                ),
              ],
              child: MyShopItem(
                deliveryFee: myShopModel[index].deliveryFee,
                grade: myShopModel[index].grade,
                id: myShopModel[index].id,
                img: myShopModel[index].image,
                iniDeliveryFee: myShopModel[index].iniDeliveryFee,
                name: myShopModel[index].name,
                myonTap: () {
                  Navigator.of(context).push(MaterialPageRoute(
                      builder: (BuildContext context) => ShopDetail(
                            shopid: myShopModel[index].id,
                          )));
                },
                padding: 0,
              ));
        },
      );
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        backgroundColor: Colors.grey[300],
        appBar: AppBar(
          centerTitle: true,
          title: Text('我的药店'),
        ),
        body: _childLayout(),
        floatingActionButton: Align(
          alignment: Alignment.bottomCenter,
          child: Container(
              child: Text(
            '左滑可下架或管理药店~',
            style: TextStyle(color: Colors.grey[500]),
          )),
        ));
  }
}
